import 'package:get/get.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/locale_tool.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'state.dart';

class SettingsMoreLogic extends BaseController {
  final SettingsMoreState state = SettingsMoreState();
  static const methodSetScreenOnDuration = "setScreenOnDuration";
  static const methodSendFindPhoneSwitchStatus = "sendFindPhoneSwitchStatus";
  static const methodSetTimeDisplay = "setTimeDisplay";
  static const methodSetFindMyPhone = "setFindMyPhone";

  @override
  void onInit() {
    super.onInit();
    addStorageListener(UTEPedConst.keyLanguage, (value) {
      _updateLanguageName();
    }, global: true);
  }

  // 获取亮屏的显示文本
  getSelectItemText() {
    return state.brightenScreenSelectChildren[state.selectedValue.value];
  }

  void setScreenOnDuration(value) {
    showLoading();
    state.prevSelected = state.selectedValue.value;
    state.selectedValue.value = value;
    var duration = state.durations[value];
    bRemoteReqAsync(methodSetScreenOnDuration, {UTEPedConst.keyValue: duration}).then((success) => _onSetScreenOnDuration(success, value));
  }

  void setTimeDisplay(value) {
    state.is24HR.value = value;
    bRemoteReqAsync(methodSetTimeDisplay, {UTEPedConst.keyTimeFormat: value ? 2 : 1}).then((success) => _onSetTimeDisplay(success, value));
  }

  void toggleSetFindMyPhone() {
    /// 1:要求服务端报警 2:要求服务端停止报警
    state.findMyPhone = (state.findMyPhone == 2 ? 1 : 2);
    bRemoteReqAsync(methodSetFindMyPhone, {UTEPedConst.keyValue: state.findMyPhone})
        .then((value) => showTips(LocaleKeys.info.ts, value ? LocaleKeys.find_success.ts : LocaleKeys.find_failed.ts));
  }

  Future<void> toLanguageSettingPage(name) async {
    await Get.toNamed(name);
    _updateLanguageName();
    update();
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    state.selectedValue.value = read(methodSetScreenOnDuration, defaultValue: 0);
    state.is24HR.value = read(UTEPedConst.keyTimeFormat, defaultValue: false);
    state.enableFindMyPhone.value = read(methodSendFindPhoneSwitchStatus, defaultValue: false);
    state.findMyPhone = 2;
    _updateLanguageName();
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }

  void _onSetScreenOnDuration(bool success, value) {
    cancelLoading();
    if (success) {
      save(methodSetScreenOnDuration, value);
    } else {
      state.selectedValue.value = state.prevSelected;
    }
  }

  void _onSetTimeDisplay(success, value) {
    if (success) {
      save(UTEPedConst.keyTimeFormat, value);
    } else {
      state.is24HR.value = !value;
    }
  }

  void _updateLanguageName() {
    var followSysLang = read(UTEPedConst.keyLanguageFollowSystem, defaultValue: false, global: true);
    var name = followSysLang ? LocaleKeys.device_language.ts : read(UTEPedConst.keyLanguageName, defaultValue: '', global: true);
    state.selectedLanguageName.value = name;
  }

  setFindMyPhone(bool value) {
    state.enableFindMyPhone.value = value;
    showLoading();
    bRemoteReqAsync(methodSendFindPhoneSwitchStatus, {UTEPedConst.keyEnable: value}).then((value) => {
      cancelLoading(),
      if (value) {
        save(methodSendFindPhoneSwitchStatus, value)
      }
    });
  }
}
